// localNotifi.js
function setupAlarmClock(){
	var currentTime = new Date().getTime();
	var time = currentTime + 60000;
	api.notification({
		vibrate: [500,500],
		light: true,
	    notify: {
	        content: '每日签到提醒',
	        extra : {
	        	extra : {
	        		msgid : getMsgid(),
		        	title : '消息提醒',
		        	content : '每日签到提醒',
		        	basetype : 1,
		        	subtype : 0
	        	}
	        }
	    },
	    alarm: {
	    	// hour: 17,
	    	// minutes: 49,
	     //    daysOfWeek: [1, 2, 3, 4, 5]
	     	time: time
	    }
	}, function(ret, err) {
		alert(JSON.stringify(ret) + '\n' + JSON.stringify(err));
	});
}
function loadNotifiStatus(){
	var usertype = $api.getStorage('userType');
	if (usertype == 0) {//如果是学生
		setupNotifiWithType(0);//设置签到通知
		setupNotifiWithType(1);//设置周报通知
		setupNotifiWithType(2);
	}
}
function setupNotifiWithType(type){
	if (type == 0) {//签到
		var signTime = currentDayTimeWithHourMinutes(9, 30);
		var currentTime = new Date().getTime();
		if (currentTime <= signTime) {//签到时间大于当前时间
			var timeDifference = signTime - currentTime;
			setTimeout(function(){
				sendNotifiWithType(0);
			}, timeDifference);
		}
	}else if(type == 1){//周报
		var weeklyTime = currentWeekTimeWithDayHourMinutes(5,17,30);
		var currentTime = new Date().getTime();
		if (currentTime <= weeklyTime) {//周报时间大于当前时间
			var timeDifference = weeklyTime - currentTime;
			setTimeout(function(){
				sendNotifiWithType(1);
			}, timeDifference);
		}
	}else{//月报
		var monthTime = getCMLastDayTimeStamp(14, 30);
		var currentTime = new Date().getTime();
		if (currentTime <= monthTime) {//月报时间大于当前时间
			var timeDifference = monthTime - currentTime;
			setTimeout(function(){
				sendNotifiWithType(2);
			}, timeDifference);
		}
	}
}
function sendNotifiWithType(type){
	cfnetppPOST(urlNotifiStatus, {
		uid: $api.getStorage('cfnetppuid'),
		jid: $api.getStorage('cfnetppjid'),
		time: timeStamp10(new Date()),
		type: type
	}, false, function(data, status){
		if (status == 'success') {
			if (data.status == 1) {
			}
		}
	});
}
function setupSignNotifi(signstatus){
	if (signstatus == 1) {
		api.notification({
			vibrate: [500,500],
			light: true,
		    notify: {
		        content: '每日签到提醒',
		        extra : {
		        	extra : {
			        	msgid : getMsgid(),
			        	title : '消息提醒',
			        	content : '每日签到提醒',
			        	basetype : 1,
			        	subtype : 0
			        }
		        }
		    },
		    alarm: {
		    	hour: 09,
		    	minutes: 30,
		        daysOfWeek: [1, 2, 3, 4, 5]
		    }
		}, function(ret, err) {
			if (ret) {
				$api.setStorage('signNotifiID', ret.id);
			}
		});
	}
}
function setupWeeklyNotifi(weeklystatus){
	if (weeklystatus == 1) {
		api.notification({
			vibrate: [500,500],
			light: true,
		    notify: {
		        content: '提交周报提醒',
		        extra : {
		        	extra : {
			        	msgid : getMsgid(),
			        	title : '消息提醒',
			        	content : '提交周报提醒',
			        	basetype : 1,
			        	subtype : 3
			        }
		        }
		    },
		    alarm: {
		    	hour: 17,
		    	minutes: 30,
		        daysOfWeek: [5]
		    }
		}, function(ret, err) {
			if (ret) {
				$api.setStorage('weeklyNotifiID', ret.id);
			}
		});
	}
}
function setupMonthNotifi(monthstatus){
	if (monthstatus == 1) {
		var time = getCMLastDayTimeStamp(14, 30);
		api.notification({
			vibrate: [500,500],
			light: true,
		    notify: {
		        content: '提交月报提醒',
		        extra : {
		        	extra : {
			        	msgid : getMsgid(),
			        	title : '消息提醒',
			        	content : '提交月报提醒',
			        	basetype : 1,
			        	subtype : 4
			        }
		        }
		    },
		    alarm: {
		    	time: time
		    }
		}, function(ret, err) {
			if (ret) {
				$api.setStorage('monthNotifiID', ret.id);
			}
		});
	}
}
function getMsgid(){
	var localmsgid = $api.getStorage('localmsgid');
	var msgid;
	if (localmsgid != '' && localmsgid != null && localmsgid != undefined && localmsgid != 0) {
		msgid = parseInt(localmsgid);
	}else{
		msgid = 100000;
	}
	msgid++;
	$api.setStorage('localmsgid', msgid);
	return msgid;
}
// function test(){
// 	var currentTime = new Date().getTime() + 10000;
// 	var alarmNotification = api.require('alarmNotification');
// 	var params = {
// 		content: '签到提醒',
// 		interval: currentTime,
// 		isViberate: true,
// 	    isLed: true
// 	};
// 	alert(JSON.stringify(params));
// 	alarmNotification.setAlarm(
// 		params,
// 		function(ret, err) {
// 	    if (ret) {
// 	        alert(JSON.stringify(ret));
// 	    } else {
// 	        alert(JSON.stringify(err));
// 	    }
// 	});
// }

